package com.k.comnote.activity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.k.comnote.R;
import com.k.comnote.adapter.FragAdapter;
import com.k.comnote.adapter.MyAdapterForDrawer;
import com.k.comnote.component.ArcMenu;
import com.k.comnote.database.NoteDB;
import com.k.comnote.model.ItemDrawer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {


    private Toolbar toolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private ListView leftDrawer;
    private List<ItemDrawer> mLeftItemList;
    private List<Fragment> fragments;
    private FragAdapter mFragAdapter;
    private ViewPager mViewPager;
    private TextView pagerText_1;
    private TextView pagerText_2;
    private TextView pagerText_3;
    private ImageView tabLine;
    private NoteDB noteDB;
    private int mScreenMargin;
    private Fragment_3 mFragment_3;
    private ArcMenu arcMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        leftDrawer = (ListView) findViewById(R.id.left_drawer);
        toolbar = (Toolbar) findViewById(R.id.tl_custom);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_left);
        pagerText_1 = (TextView) findViewById(R.id.pager_tv_1);
        pagerText_2 = (TextView) findViewById(R.id.pager_tv_2);
        pagerText_3 = (TextView) findViewById(R.id.pager_tv_3);
        tabLine = (ImageView) findViewById(R.id.tabline);

        pagerText_1.setOnClickListener(this);
        pagerText_2.setOnClickListener(this);
        pagerText_3.setOnClickListener(this);

        /**
         * 卫星菜单相关
         */

        arcMenu = (ArcMenu) findViewById(R.id.arcmenu);
        arcMenu.setOnMenuItemClickListener(new ArcMenu.OnMenuItemClickListener() {
            @Override
            public void onClick(View view, int pos) {
                switch (view.getId()) {
                    case R.id.arcmenu_note :
                        noteFunction();
                        break;
                    case R.id.arcmenu_record :

                        break;
                    case R.id.arcmenu_location :

                        break;
                    case R.id.arcmenu_camera :

                        break;

                }
            }


        });


        /**
         *滑动条相关
         */
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreenMargin = outMetrics.widthPixels / 3;
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tabLine.getLayoutParams();
        lp.width = mScreenMargin;
        tabLine.setLayoutParams(lp);


        /**
         * Actionbar与Drawerlayout
         */
        toolbar.setTitle("个人笔记");
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); //创建返回键，并实现打开关/闭监听
        getSupportActionBar().setDisplayShowHomeEnabled(false);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        /**
         * leftDrawer相关
         */

        mLeftItemList = new ArrayList<>();
        mLeftItemList.add(new ItemDrawer(R.drawable.list_messages, "消息中心"));
        mLeftItemList.add(new ItemDrawer(R.drawable.list_my_note, "我的日记"));
        leftDrawer.setAdapter(new MyAdapterForDrawer(this, mLeftItemList));

        /**
         *ViewPager相关
         */

        fragments = new ArrayList<>();
        mFragment_3 = new Fragment_3();
        fragments.add(new Fragment_1());
        fragments.add(new Fragment_2());
        fragments.add(mFragment_3);

        mFragAdapter = new FragAdapter(getSupportFragmentManager(), fragments);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(mFragAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tabLine.getLayoutParams();
                lp.leftMargin = (int) (position * mScreenMargin + positionOffset * mScreenMargin);
                tabLine.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                pagerText_1.setTextColor(getResources().getColor(R.color.color_tab_text_normal));
                pagerText_2.setTextColor(getResources().getColor(R.color.color_tab_text_normal));
                pagerText_3.setTextColor(getResources().getColor(R.color.color_tab_text_normal));
                switch (position) {
                    case 0:
                        pagerText_1.setTextColor(getResources().getColor(R.color.color_tab_text_selected));
                        break;
                    case 1:
                        pagerText_2.setTextColor(getResources().getColor(R.color.color_tab_text_selected));
                        break;
                    case 2:
                        pagerText_3.setTextColor(getResources().getColor(R.color.color_tab_text_selected));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 卫星菜单菜单功能
     */
    private void noteFunction() {
        /**
         * 让卫星菜单的动画播放完毕，所以延时启动activity
         */
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent keepNote = new Intent(getApplication(), NoteKeep.class);
                startActivity(keepNote);
            }
        }, 500);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pager_tv_1:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.pager_tv_2:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.pager_tv_3:
                mViewPager.setCurrentItem(2);
                break;
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.ac_bar_add_tag:
                addTagDialog();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void addTagDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.addtagdialog, null);
        final EditText newTag = (EditText) view.findViewById(R.id.edittag);
        builder.setView(view);
        builder.setTitle("新建标签")
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String newTagName = newTag.getText().toString();

                                mFragment_3.update(newTagName);
                            }
                        }
                )
                .create()
                .show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


}
